home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / SuperMarquee 1.0.2 / SuperMarquee.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  4.5 KB  |  173 lines  |  [TEXT/CWIE]

  1. // SuperMarquee
  2. // version 1.0.2
  3. // ported to CodeWarrior by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 on 951201
  5.  
  6. #define NIL 0L
  7.  
  8. BitMap    StringToBitMap (Str255 s, int h, int v, int descent);
  9. void    InitManagers (void);
  10. void MarqueeDisplay (Str255 s, int displayWidth);
  11. void MarqueeIdle (void);
  12. void MarqueeInit (void);
  13.  
  14. typedef struct MarqueeRec {
  15.     struct MarqueeRec    *fNext;
  16.     int            fType;
  17.     Rect        fFromRect, fToRect, newRect;
  18.     BitMap        fBits;
  19.     int            fOffset;
  20.     int            fLeftMaximum;
  21.     long        fDelayTimer;
  22.     WindowPtr    fMarqueePort;
  23. } MarqueeRec, *MarqueePtr;
  24.  
  25. QHdr    gMarqueeQueue;
  26.  
  27. main ()
  28. {
  29.     Rect        r;
  30.     WindowPtr    w;
  31.     EventRecord e;
  32.     Str255        s1 = "\p This string is too long. ", 
  33.                 s2 = "\p This string is also extremely way, way, too long!!! ",
  34.                 s3 = "\p If you can read this you are driving too close for proper traffic operational safety and personal well being! ",
  35.                 s4 = "\p Four score and seven years ago, our forefathers brought forth upon this continent a new nation.  Conceived in liberty............ ",
  36.                 s5 = "\p SUPERMAN!  Strange visitor from another planet, who came to Earth, with powers and abilities far beyond those of mortal men! ";
  37.     
  38.     InitManagers ();
  39.     MarqueeInit ();
  40.     SetRect (&r, 40, 40, 240, 290);
  41.     w = NewWindow (NIL, &r, "\pMarquee Display", true, rDocProc, (WindowPtr)-1L, false, 0);
  42.     SetPort (w);
  43.     TextFont (geneva);
  44.     MoveTo (100, 30);
  45.     MarqueeDisplay (s1, 100);
  46.     TextFont (0);
  47.     MoveTo (100, 60);
  48.     MarqueeDisplay (s2, 100);
  49.     TextFont (monaco);
  50.     TextSize (7);
  51.     MoveTo (100, 85);
  52.     MarqueeDisplay (s3, 100);
  53.     TextFont (courier);
  54.     TextSize (48);
  55.     MoveTo (100, 138);
  56.     MarqueeDisplay (s4, 100);
  57.     TextFont (0);
  58.     TextSize (48);
  59.     MoveTo (100, 218);
  60.     MarqueeDisplay (s5, 100);
  61.     while (! GetNextEvent (mDownMask, &e) )
  62.         MarqueeIdle ();
  63. }
  64.  
  65. void    InitManagers (void)
  66. {
  67.     InitGraf (&qd.thePort);
  68.     InitFonts ();
  69.     InitWindows ();
  70.     InitMenus ();
  71.     TEInit ();
  72.     InitDialogs (NIL);
  73.     FlushEvents (everyEvent, 0);
  74.     InitCursor ();
  75. }
  76.  
  77. /***************************/
  78. BitMap    StringToBitMap (Str255 s, int h, int v, int descent)
  79. {
  80.     GrafPtr        saved;
  81.     GrafPort    newPort;
  82.     Rect        r;
  83.  
  84.     GetPort (&saved);
  85.     OpenPort (&newPort);
  86.     SetRect (&r, 0, 0, h, v);
  87.     BlockMove (&r, &newPort.portRect, sizeof (Rect));
  88.  
  89.     RectRgn (newPort.visRgn, &r);
  90.     RectRgn (newPort.clipRgn, &r);
  91.     newPort.portBits.rowBytes = ((h + 15) / 16) * 2;
  92.     newPort.portBits.baseAddr = NewPtr (v * newPort.portBits.rowBytes );
  93.     BlockMove (&r, &newPort.portBits.bounds, sizeof (Rect));
  94.  
  95.     EraseRect (&r);
  96.     MoveTo (0, v - descent);
  97.     TextFont (saved->txFont);
  98.     TextFace (saved->txFace);
  99.     TextSize (saved->txSize);
  100.     DrawString (s);
  101.  
  102.     SetPort (saved);
  103.     return newPort.portBits;
  104. }
  105. /***************************/
  106. void MarqueeInit ()
  107. {
  108.   gMarqueeQueue.qFlags = 0;
  109.   gMarqueeQueue.qHead = NIL;
  110.   gMarqueeQueue.qTail = NIL;
  111. }  
  112. /***************************/
  113. void MarqueeIdle (void )
  114. {
  115.     MarqueePtr    thisRec;
  116.     
  117.     thisRec = (MarqueePtr) gMarqueeQueue.qHead;
  118.     for (; thisRec != NIL; thisRec = thisRec->fNext )
  119.         if (thisRec->fOffset != 0 && TickCount () > thisRec->fDelayTimer)
  120.         {
  121.             thisRec->fDelayTimer = TickCount ();// + 2;
  122.             
  123.             CopyBits (&thisRec->fBits, &thisRec->fMarqueePort->portBits, &thisRec->fFromRect, &thisRec->fToRect, srcCopy, NIL);
  124.             
  125.             if (thisRec->fFromRect.left + thisRec->fOffset < 0 || thisRec->fFromRect.left + thisRec->fOffset > thisRec->fLeftMaximum) 
  126.             {
  127.                 thisRec->fOffset = -thisRec->fOffset;
  128.                 thisRec->fDelayTimer = TickCount ();// + 40;
  129.             }
  130.             else
  131.                 OffsetRect (&thisRec->fFromRect, thisRec->fOffset, 0);
  132.         }
  133. }
  134. /***************************/
  135. void MarqueeDisplay (Str255 s, int displayWidth)
  136. {
  137.     int            sWidth, dH, lineHeight;
  138.     Point        curLoc;
  139.     FontInfo    fontstuff;
  140.     MarqueePtr    newRec;
  141.     
  142.     sWidth = StringWidth (s);
  143.     if (sWidth < displayWidth) 
  144.     {
  145.         Move (- (sWidth / 2), 0);
  146.         DrawString (s);
  147.      }
  148.     else
  149.         {
  150.             newRec = (MarqueePtr) (NewPtrClear (sizeof (MarqueeRec) + 4) + 4);
  151.             
  152.             GetFontInfo (&fontstuff);
  153.             
  154.             lineHeight = fontstuff.ascent + fontstuff.descent + fontstuff.leading;
  155.             newRec->fBits = StringToBitMap (s, sWidth, lineHeight, fontstuff.descent);
  156.             
  157.             SetRect (&newRec->fFromRect, 0, 0, displayWidth, lineHeight);
  158.             
  159.             BlockMove (&newRec->fFromRect, &newRec->fToRect, sizeof (Rect));
  160.             GetPen (&curLoc);
  161.             OffsetRect (&newRec->fToRect, curLoc.h - displayWidth / 2, curLoc.v - fontstuff.ascent);
  162.             InsetRect (&newRec->fToRect, -2, -2);
  163.             FrameRect (&newRec->fToRect);
  164.             InsetRect (&newRec->fToRect, 2, 2);
  165.             newRec->fLeftMaximum = sWidth - displayWidth;
  166.             newRec->fOffset = 1;
  167.             newRec->fDelayTimer = TickCount ();// +1;
  168.             GetPort (&newRec->fMarqueePort);
  169.             
  170.             Enqueue ((QElemPtr) newRec, &gMarqueeQueue );
  171.      }
  172. }
  173.